package cn.diancan.Tags.freemarker;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map; 

import freemarker.core.Environment;
import freemarker.template.TemplateDirectiveBody;
import freemarker.template.TemplateDirectiveModel;
import freemarker.template.TemplateException;
import freemarker.template.TemplateModel;

public class DateFormateDirectiveModel implements TemplateDirectiveModel {

	@SuppressWarnings("rawtypes")
	public void execute(Environment env, Map params, TemplateModel[] loopVars,
			TemplateDirectiveBody body) throws TemplateException, IOException {
		Long date_l = Long.valueOf( params.get("date").toString());
		String pattern=  params.get("pattern").toString();
		env.getOut().write(dataFormat(new Date(date_l), pattern)); 
	}
	
	public String dataFormat(Date date, String pattern){
		return (new SimpleDateFormat(pattern)).format(date);
	}

}
